# Maintainer: Milan P. Stanić <mps@arvanta.net>
# this package is temporary for asahi linux
pkgname=mesa-asahi
_asahiver=20240228
_commit=asahi-$_asahiver
pkgver=24.0.0_pre$_asahiver
pkgrel=1
pkgdesc="Mesa DRI OpenGL library for Asahi linux"
url="https://gitlab.freedesktop.org/asahi/mesa"
arch="aarch64"
license="MIT SGI-B-2.0 BSL-1.0"
subpackages="
	$pkgname-dev
	$pkgname-dri-gallium:_gallium
	$pkgname-va-gallium:_va
	$pkgname-vdpau-gallium:_vdpau
	$pkgname-glapi
	$pkgname-egl
	$pkgname-gl
	$pkgname-gles
	$pkgname-xatracker
	$pkgname-osmesa
	$pkgname-gbm
	$pkgname-vulkan-swrast:_vulkan
	$pkgname-rusticl
	"
_llvmver=17
depends_dev="
	libdrm-dev
	libxext-dev
	libxdamage-dev
	libxcb-dev
	libxshmfence-dev
	"
makedepends="
	$depends_dev
	bison
	clang17-dev
	expat-dev
	findutils
	flex
	gettext
	elfutils-dev
	glslang-dev
	libtool
	libclc-dev
	libxfixes-dev
	libva-dev
	libvdpau-dev
	libx11-dev
	libxml2-dev
	libxrandr-dev
	libxxf86vm-dev
	llvm$_llvmver-dev
	meson
	py3-mako
	python3
	vulkan-loader-dev
	wayland-dev
	wayland-protocols
	xorgproto
	zlib-dev
	zstd-dev
	rust
	rust-bindgen
	spirv-llvm-translator-dev
	"
source="
	https://gitlab.freedesktop.org/asahi/mesa/-/archive/$_commit/mesa-$_commit.tar.gz
	"
#	$pkgname-$_commitid.tar.gz::https://gitlab.freedesktop.org/asahi/mesa/-/archive/$_commitid/mesa-$_commitid.tar.gz

replaces="mesa-dricore"
options="!check" # we skip tests intentionally
builddir="$srcdir"/mesa-asahi-$_asahiver

_dri_driverdir=/usr/lib/xorg/modules/dri
_gallium_drivers="swrast,virgl,kmsro,asahi,svga,zink"
_vulkan_drivers="swrast"
_vulkan_layers="device-select,overlay"
	_rusticl=true

	# ~5% smaller
	_lto=false

build() {
	# use -g1 to generate less debug info:
	# 485 MiB -> ~80 MiB
	export CFLAGS="$CFLAGS -O2 -g1"
	export CXXFLAGS="$CXXFLAGS -O2 -g1"
	export CPPFLAGS="$CPPFLAGS -O2 -g1"
	echo '24.0.0' > VERSION
	case "$CARCH" in
	armhf|armv7)
		# gnu2 tlsdesc is broken in binutils
		export CFLAGS="$CFLAGS -mtls-dialect=gnu"
		export CXXFLAGS="$CXXFLAGS -mtls-dialect=gnu"
		;;
	esac

	PATH="$PATH:/usr/lib/llvm$_llvmver/bin" \
	abuild-meson \
		-Db_ndebug=true \
		-Db_lto=$_lto \
		-Ddri-drivers-path=$_dri_driverdir \
		-Dgallium-drivers=$_gallium_drivers \
		-Dvulkan-drivers=$_vulkan_drivers \
		-Dvulkan-layers=$_vulkan_layers \
		-Dplatforms=x11,wayland \
		-Dllvm=enabled \
		-Dshared-llvm=enabled \
		-Dshared-glapi=enabled \
		-Dgbm=enabled \
		-Dglx=dri \
		-Dopengl=true \
		-Dosmesa=true \
		-Dgles1=enabled \
		-Dgles2=enabled \
		-Ddri3=enabled \
		-Degl=enabled \
		-Dgallium-extra-hud=true \
		-Dgallium-nine=false \
		-Dgallium-rusticl=$_rusticl \
		-Dgallium-va=enabled \
		-Dgallium-vdpau=enabled \
		-Dgallium-xa=enabled \
		-Dvideo-codecs=h264dec,h264enc,h265dec,h265enc \
		. output

	# Print config
	meson configure --no-pager output

	# parallel build workaround
	ninja -C output \
		src/compiler/nir/nir_intrinsics.h \
		src/util/format/u_format_pack.h \
		$build_first

	meson compile -C output
}

package() {
	DESTDIR="$pkgdir" meson install --no-rebuild -C output
	provides="mesa"
	provider_priority=0
	replaces="mesa"

}

_dev() {
	pkgdesc="Mesa  development libraries"
	depends="mesa-asahi=$pkgver-r$pkgrel"
	provides="mesa-dev"
	provider_priority=0
	replaces="mesa-dev"
}
egl() {
	pkgdesc="Mesa libEGL runtime libraries"
	depends="mesa-asahi=$pkgver-r$pkgrel"
	provider_priority=0
	provides="mesa-egl"
	replaces="mesa-egl"

	amove usr/lib/libEGL.so*
}

gl() {
	pkgdesc="Mesa libGL runtime libraries"
	depends="mesa-asahi=$pkgver-r$pkgrel"
	provider_priority=0
	provides="mesa-gl"
	replaces="mesa-gl"

	amove usr/lib/libGL.so*
}

glapi() {
	pkgdesc="Mesa shared glapi"
	depends="mesa-asahi=$pkgver-r$pkgrel"
	provider_priority=0
	provides="mesa-glapi"
	replaces="mesa-glapi"

	amove usr/lib/libglapi.so.*
}

gles() {
	pkgdesc="Mesa libGLESv2 runtime libraries"
	depends="mesa-asahi=$pkgver-r$pkgrel"
	provider_priority=0
	provides="mesa-gles"
	replaces="mesa-gles"

	amove usr/lib/libGLES*.so*
}
xatracker() {
	pkgdesc="Mesa XA state tracker for vmware"
	depends="mesa-asahi=$pkgver-r$pkgrel"
	provider_priority=0
	provides="mesa-xatracker"
	replaces="mesa-xatracker"

	amove usr/lib/libxatracker*.so.*
}

osmesa() {
	pkgdesc="Mesa offscreen rendering libraries"
	depends="mesa-asahi=$pkgver-r$pkgrel"
	provider_priority=0
	provides="mesa-osmesa"
	replaces="mesa-osmesa"

	amove usr/lib/libOSMesa.so.*
}

gbm() {
	pkgdesc="Mesa gbm library"
	depends="mesa-asahi=$pkgver-r$pkgrel"
	provider_priority=0
	provides="mesa-gbm"
	replaces="mesa-gbm"

	amove usr/lib/libgbm.so.*
}

libd3dadapter9() {
	pkgdesc="Mesa directx9 adapter"
	depends="mesa-asahi=$pkgver-r$pkgrel"
	provider_priority=0

	amove usr/lib/d3d/d3dadapter9.so*
}

rusticl() {
	pkgdesc="Mesa OpenCL driver"
	depends="mesa-asahi=$pkgver-r$pkgrel clang$_llvmver-headers libclc~$_llvmver"
	provider_priority=0
	provides="mesa-rusticl"
	replaces="mesa-rusticl"

	amove usr/lib/libRusticlOpenCL.so.*
	amove etc/OpenCL/vendors/
}

# Move links referencing the same file to the subpackage.
# Usage: _mv_links <base directory> <example>
# where <example> is one of the libraries covered by the megadriver.
# The example is used to find other links that point to the same file.
_mv_links() {
	install -d "$subpkgdir"/$1
	find -L "$pkgdir"/$1 -samefile "$pkgdir"/$1/$2 -print0 \
		| xargs -0 -I{} mv {} "$subpkgdir"/$1/
}

_mv_vulkan() {
	local i
	install -d "$subpkgdir"/usr/lib
	install -d "$subpkgdir"/usr/share/vulkan/icd.d
	for i in "$@"; do
		amove usr/lib/libvulkan_$i*.so
		amove usr/share/vulkan/icd.d/${i}_*.*
	done
}

# Mesa uses "megadrivers" where multiple drivers are linked into one shared
# library. This library is then hard-linked to separate files (one for each driver).
# Each subpackage contains one megadriver so that all the hard-links are preserved.

_gallium() {
	pkgdesc="Mesa gallium DRI drivers"
	depends="mesa-asahi=$pkgver-r$pkgrel"
	provider_priority=0
	provides="mesa-dri-gallium"
	replaces="mesa-dri-gallium"

	# libgallium_dri.so
	_mv_links $_dri_driverdir swrast_dri.so
}

_va() {
	local n=${subpkgname##*-va-}
	pkgdesc="Mesa $n VAAPI drivers"
	depends="mesa-asahi=$pkgver-r$pkgrel libva"

	case $n in
	gallium)
		# libgallium_drv_video.so
		_mv_links /usr/lib/dri radeonsi_drv_video.so ;;
	esac
}

_vdpau() {
	local n=${subpkgname##*-vdpau-}
	pkgdesc="Mesa $n VDPAU drivers"
	depends="mesa-asahi=$pkgver-r$pkgrel libvdpau"

	case $n in
	gallium)
		# libvdpau_gallium.so.1.0.0
		_mv_links /usr/lib/vdpau libvdpau_radeonsi.so.1.0.0 ;;
	esac
}

_vulkan() {
	local n=${subpkgname##*-vulkan-}
	pkgdesc="Mesa Vulkan API driver for $n"
	depends="mesa-asahi=$pkgver-r$pkgrel"
	provider_priority=0
	provides="mesa-vulkan"
	replaces="mesa-vulkan"

	case $n in
	swrast)
		_mv_vulkan lvp ;;
	esac
}

_vulkan_layers() {
	pkgdesc="collection of vulkan layers from mesa"
	depends="python3"
	provider_priority=0
	provides="mesa-vulkan-layers"

	# Remove this after the release of the next stable (3.14)
	# it originally was claed layer as it only packaged the
	# overlay one but now it also packages device-select and
	# intel-nullhw (on x86*)
	provides="$pkgname-vulkan-layer=$pkgver-r$pkgrel"
	replaces="$pkgname-vulkan-layer=$pkgver-r$pkgrel"

	amove usr/share/vulkan/explicit_layer.d
	amove usr/share/vulkan/implicit_layer.d
	amove usr/lib/libVkLayer_*.so
	amove usr/bin/mesa-overlay-control.py
}

sha512sums="
7c0125e67ad34b283a4aebf2236d709c3d783583f7d851b725c8fd50767ca29c10337794953e291f6aa87eac2432856e00a90e363b820edd45e500e0891f78bb  mesa-asahi-20240228.tar.gz
"
